APM.MainLoginController = Ember.Controller.extend({
	loginBtnText: "登　录",

	actions: {
		login: function() {
			var me = this,
				username = $.trim(this.get('username')),
				password = this.get('password');
			var $loginError = $(".login-error-text");
			if (username == '') {
				this.set('loginFailText', '请输入用户名');
				$loginError.css("top","100px");
			} else if ( ! password) {
				this.set('loginFailText', '请输入密码');
				$loginError.css("top","165px");
			} else {

				// this.store.reopen({
				// 	alertTip: function(message) {
				// 		me.set('loginFailText', message);
				// 		console.log("1"+message);
				// 	}
				// });

				this.store.ajax("POST", "authentication/login", {
					account: username,
					password: password
				}).then(function(response) {
					if (response.status == "fail") {
						me.set('loginFailText', response.message);
						//this.set("password", "");
					} else {
						me.set('loginFailText', '');

						var data = {
							id:response.user.id,
							token: response.meta.token,
							account: response.user.account,
							name: response.user.name,
							role: response.user.role
						};

						for (var name in data) {
							$.cookie(name, data[name], '/');
						}

						me.set("auth.user", response.user);

						var redirectURL = APM.util.getUrlParams("redirectURL");
						$('.sk-fading-circle').show();
						$('button').attr("disabled",true);
						$('input').attr("disabled",true);
						setTimeout(function(){
							if(redirectURL){
								location.href = redirectURL;
							}else{
								me.transitionToRoute("business");
							}
						},500);
					}
				});
			}
		}
	}
});